/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ez2find.manager;

import com.ez2find.model.Area;
import com.ez2find.model.Personal;
import com.ez2find.model.PersonalCategorie;
import com.ez2find.model.RegisterUser;
import com.ez2find.model.Site;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Owner
 */
@Stateless
public class PersonalManagerBean implements PersonalManagerLocal {
    
    @PersistenceContext
    private EntityManager em;
    
    public void create(Personal personal)throws Exception {
        em.persist(personal);
    }
 
    public void update(Personal personal)throws Exception {
        em.merge(personal);
    }
    
    public Personal merge(Personal personal)throws Exception {
        return em.merge(personal);
    }
    
    public void delete(Personal personal)throws Exception {
        personal=em.merge(personal);
        em.remove(personal);
    }
    
    public Personal find(long id){
        return em.find(Personal.class,id);
    }

    public List<Personal> listAll(){
        Query query=em.createNamedQuery("listAllPersonals");
        List<Personal> result=query.getResultList();
        return result;
    }
    
    public long lastCode(){
        try{
            Query query=em.createNamedQuery("lastPersonalCode");
            return (Long) query.getSingleResult();
        }catch(Exception ex){
            return 0;
        }
    }
    
    public List<Personal> listPersonalsSite(Site site, boolean personalC, PersonalCategorie personalCategorie, String consult){
        Query query=em.createQuery("SELECT p FROM Personal p WHERE p.site=:site AND p.status='active'"+consult+" ORDER BY p.publishDate DESC, p.code DESC");
        query.setParameter("site", site);
        if(personalC)
            query.setParameter("personalCategorie", personalCategorie);
        List<Personal> result=query.getResultList();
        return result;
    }
    
    public List<Personal> listPersonalsArea(Area area, boolean personalC, PersonalCategorie personalCategorie, String consult){
        Query query=em.createQuery("SELECT p FROM Personal p WHERE p.area=:area AND p.status='active'"+consult+" ORDER BY p.publishDate DESC, p.code DESC");
        query.setParameter("area", area);
        if(personalC)
            query.setParameter("personalCategorie", personalCategorie);
        List<Personal> result=query.getResultList();
        return result;
    }
    
}
